import Utils from "../../../../Utils";
import MainHome from "../../../facility/priveteFacility/MainHome";
import { FacilityType, Gain } from "../../../info/GameEnum";
import { Location } from "../../../info/Predefine";
import GameManager from "../../../manager/GameManager";
import { AiEnvironment } from "../AiGameAction";
import { DecisionNode } from "../DecisionTree";

export default class DecisionDomain extends DecisionNode {

    environment: AiEnvironment

    constructor(environment: any) {
        super(environment);
        this.childDecisionNodes.push(new DecisionBuildDomainExpander(environment));
        this.childDecisionNodes.push(new DecisionMainHomeLevelUp(environment));
    }

    /**
     * 对领地的需求
     * 当前可建造的设施 可建造地的数量 
     * @returns 
     */
    evaluate(): number {
        let score = 100;
        let spaces: Location[][];
        if (this.environment.player.checkFacilityCondition(FacilityType.火箭炮基地).every(item => item)
            || this.environment.player.checkFacilityCondition(FacilityType.电磁炮基地).every(item => item)
            || this.environment.player.checkFacilityCondition(FacilityType.核弹基地).every(item => item)
        ) {
            spaces = this.environment.player.getSpaceBySize(4);
            score = Math.max(0, score - spaces.length * 45);
        } else {
            if (this.environment.player.checkFacilityCondition(FacilityType.洲际导弹基地).every(item => item)) {
                spaces = this.environment.player.getSpaceBySize(3);
                score = Math.max(0, score - spaces.length * 30)
            } else {
                spaces = this.environment.player.getSpaceBySize(1);
                score = Math.max(0, score - spaces.length * 20);
            }
        }
        return score;
    }

}

/**
 * 领土扩张器
 */
class DecisionBuildDomainExpander extends DecisionNode {
    environment: AiEnvironment

    evaluate(): number {
        if (this.environment.player.checkFacilityCondition(FacilityType.领土扩张器)) {
            let count = this.environment.player.facilityArr[FacilityType.领土扩张器].length;
            switch (count) {
                case 0:
                    return 80;
                case 1:
                    return 40;
                case 2:
                    return 20;
                default:
                    return 10;
            }
        } else {
            return 0;
        }
    }

    execute(): number {
        //离基地越远，得分越高
        let spaces = this.environment.player.getSpaceBySize(1); 
        let mainHome = <MainHome>this.environment.player.facilityArr[FacilityType.主基地][0];
        let locaitons = spaces.map(item => item[0]);
        let mainHomLocation = GameManager.instance.getMapLocation(mainHome.node.position);
        if (this.environment.player.mainHomeLevel == 0) {
            this.environment.player.addFacility(FacilityType.领土扩张器, Utils.getRandomChoose(spaces));
        } else {
            let scores: number[] = locaitons.map((item) => {
                return Math.abs(mainHomLocation.locationX - item.locationX) + Math.abs(mainHomLocation.locationY - item.locationY);
            })
            let index = Utils.getRandomIndexByProbability(scores);
            this.environment.player.addFacility(FacilityType.领土扩张器, spaces[index]);
        }
        return 0;
    }
}

/**
 * 升级主基地
 */
class DecisionMainHomeLevelUp extends DecisionNode {
    evaluate(): number {
        if (!this.environment.player.checkGainCondition(Gain.升级主基地).every(item => item)) {
            return 0;
        }

        //有多少决策是因为基地等级不够
        let count = 0,
            size = this.environment.gainFeasibility.size + this.environment.facilityFeasibility.size;
        let addFunc = (v: boolean[]) => {
            v[2] && count++;
        }
        this.environment.gainFeasibility.forEach(addFunc);
        this.environment.facilityFeasibility.forEach(addFunc);

        return 30 + count / size * 100;
    }

    execute(): number {
        this.environment.player.MainHomeLevelUp();
        return 0;
    }
}
